package com.bskj.bms.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bskj.bms.service.IBmsInstallService;
import com.bskj.common.bean.model.NoCarnoParkRate;
import com.bskj.common.dao.NoCarnoParkRateDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.service.impl.BaseServiceImpl;

@Service
public class BmsInstallServiceImpl extends BaseServiceImpl<NoCarnoParkRate> implements IBmsInstallService{
	
	@Autowired
	private NoCarnoParkRateDao noCarnoParkRate;

	@Override
	public BaseDao<NoCarnoParkRate> getBaseDao() {
		// TODO Auto-generated method stub
		return noCarnoParkRate;
	}

	@Override
	public Map<String, Object> updateOrInsert(NoCarnoParkRate noCarnoParkRate) {
		Map<String, Object> resultMap=new HashMap<>();
		try {
			NoCarnoParkRate noCarnoParkRates=findRate(noCarnoParkRate.getParkingLotCustId());
			if (noCarnoParkRates==null) {
				noCarnoParkRate.setCreateTime(new Date());
				noCarnoParkRate.setUpdateTime(new Date());
				insert(noCarnoParkRate);
			} else {
				noCarnoParkRate.setId(noCarnoParkRates.getId());
				noCarnoParkRate.setUpdateTime(new Date());
				noCarnoParkRate.setCreateTime(noCarnoParkRates.getCreateTime());
				update(noCarnoParkRate);
			}
			resultMap.put("success",true);
			resultMap.put("meg","设置成功！");
		} catch (Exception e) {
			resultMap.put("success",false);
			resultMap.put("meg","设置失败！");
			e.printStackTrace();
			return resultMap;
		}
		return resultMap;
	}

	@Override
	public NoCarnoParkRate findRate(Integer custId) throws Exception {
		FindRequestEntity findReq=new FindRequestEntity();
		findReq.addWhere("parking_lot_cust_id", WhereEnum.EQ,custId);
		List<NoCarnoParkRate> noCarnoParkRates=find(findReq);
		if (noCarnoParkRates.size()==0) {
			return null;
		} else {
			return noCarnoParkRates.get(0);
		}
	}
}
